
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : zhangliang
 * @date : 2023-11-21
 */
public class test {

    /**
     * 注意 如果图片像素过大，生产excel会比较慢
     * 建议图片尺寸200*200以内
     */
    public static void main(String[] args) throws Exception {
        List<int[]> load = load("C:\\img\\test.png");
//        List<int[]> load = load("e:\\img\\Snipaste_2021-02-24_17-38-26.png");
        // 填入到Excel
        PicToXlsx.create(load, "C:\\img");
    }

    static List<int[]> load(String path) throws Exception {
        System.out.println("Loading picture…");
        BufferedImage bi = ImageIO.read(new File(path));

        int width = bi.getWidth();
        int height = bi.getHeight();
        int minx = bi.getMinX();
        int miny = bi.getMinY();
        int len = width - minx;

        List<int[]> pic = new ArrayList<>(height - miny);

        // 按行遍历像素点
        for (int j = miny; j < height; j++) {
            int[] array = new int[len];
            int index = 0;
            for (int i = minx; i < width; i++) {
                array[index] = bi.getRGB(i, j);
                index++;
            }
            pic.add(array);
        }

        return pic;
    }
}